package edu.chinasoft.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import edu.chinasoft.pojo.Goods;
import edu.chinasoft.service.GoodsService;
import edu.chinasoft.service.GoodsServiceImp;

/**
 * 
 * @author cjogng
 * @date 2020年1月16日:下午3:57:22
 * @version V1.0
 * @Description: 添加商品
*/
@WebServlet(name="myAddGoodsForServlet",value="/servlet/addgoodsforservlet")
public class  AddGoodsForServlet  extends HttpServlet {
	private static Logger logger = Logger.getLogger( AddGoodsForServlet .class);
	private GoodsService goodsService=new GoodsServiceImp();
	
	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" AddGoodsForServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		//1：获取界面中传递过来的值
		String goods_name = request.getParameter("goods_name");
		String goods_desc = request.getParameter("goods_desc");
		String goods_image = request.getParameter("goods_image");
		String goods_price = request.getParameter("goods_price");
		String goods_stock = request.getParameter("goods_stock");

  		
		//2：把值封装到goods对象里
		Goods goods = new Goods(goods_name,goods_desc,goods_image,Double.parseDouble(goods_price),Integer.parseInt(goods_stock));
		
		//3：调用服务器层方法把商品保存到数据库里
		boolean flag = goodsService.addGoods(goods);
		
		//4：成功之后跳转到成功页面，失败之后跳转到添加商品页面
		if(flag) {
			response.sendRedirect("/project06/addgoodssuccess.html");
		}else {
			response.sendRedirect("/project06/addgoodserror.html");
		}
	
		
		logger.debug(" AddGoodsForServlet >doGet end.......");
	}
	
	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		logger.debug(" AddGoodsForServlet >doPost start.......");
		this.doGet(request, response);
		
		logger.debug(" AddGoodsForServlet >doPost end.......");
	}
	
}